package chances.epg.search.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.JSONUtils;
import chances.epg.search.analyzer.SplitAnalyzerUtils;

public class SplitAnalyzerTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7843872465308953569L;
	private String source;
	private String var;
	private String jsonVar;

	@Override
	public int doEndTag() throws JspException {
		// this.source = this.source.replaceAll(" ", "");
		this.source = this.source.replaceAll("︳", "|");
		this.source = this.source.replaceAll("︱", "|");
		if (StringUtils.isNotBlank(this.source)) {
			String[] result = SplitAnalyzerUtils.getInstance().split(this.source);
			if (StringUtils.isNotBlank(this.var)) {
				pageContext.setAttribute(this.var, result);
			}

			if (StringUtils.isNotBlank(this.jsonVar)) {
				String json = JSONUtils.fromObject(result);
				pageContext.setAttribute(this.jsonVar, json);
			}
		}

		return super.doEndTag();
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getJsonVar() {
		return jsonVar;
	}

	public void setJsonVar(String jsonVar) {
		this.jsonVar = jsonVar;
	}
}
